﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class SearchFlight2 : System.Web.UI.Page
{
    FlightBAL _flightBAL;
    protected void Page_Load(object sender, EventArgs e)
    {
        _flightBAL = new FlightBAL();
        if (Session["origin"] == null || Session["destination"] == null || Session["DepartDates"] == null)
        {
            Response.Redirect("~/SearchFlight1.aspx");
        }
        if (!Page.IsPostBack)
        {
            Session["DepartRoute"] = null;
            Session["ReturnRoute"] = null;


            Location _origin = (Location)Session["origin"];
            Location _destination = (Location)Session["destination"];

            lblOrigin.Text = _origin.FullName;
            lblDestination.Text = _destination.FullName;

            List<DateTime> DepartTime = (List<DateTime>)Session["DepartDates"];
            List<BigRoute> DepartRoute = _flightBAL.SearchRoute(_origin, _destination, DepartTime);
            if (DepartRoute.Count == 0)
            {
                lblavail1.Enabled = true;
                lblavail1.Visible = true;
                Button1.Enabled = false;
            }
            else
            {
                Session["DepartRoute"] = DepartRoute;
                lblNumofStopDepart.Text = (DepartRoute[0].RouteList.Count - 1).ToString();
                if (DepartRoute[0].RouteList.Count > 1)
                {
                    lblNumofStopDepart.Text += " (No Direct flight available yet)";
                }
                for (int i = 0; i < DepartRoute.Count; i++)
                {
                    string text = "";
                    foreach (SmallRoute route in DepartRoute[i].RouteList)
                    {
                        text += (route.Origin.FullName + " > ");
                    }
                    text += (DepartRoute[i].RouteList[DepartRoute[i].RouteList.Count - 1].Destination.FullName);
                    ListItem item = new ListItem(text, i.ToString());
                    rdbRouteDepart.Items.Add(item);
                    rdbRouteDepart.SelectedIndex = 0;
                }
            }


            if (Session["ReturnDates"] != null)
            {
                lblFlight.Text = "Round Trip";
                PlaceHolder1.Visible = true;

                List<DateTime> ReturnTime = (List<DateTime>)Session["ReturnDates"];
                List<BigRoute> ReturnRoute = _flightBAL.SearchRoute(_destination, _origin, ReturnTime);
                if (ReturnRoute.Count == 0)
                {
                    lblavail2.Enabled = true;
                    lblavail2.Visible = true;
                    Button1.Enabled = false;
                }
                else
                {
                    Session["ReturnRoute"] = ReturnRoute;
                    lblNumofStopReturn.Text = ReturnRoute[0].RouteList.Count.ToString();
                    if (ReturnRoute[0].RouteList.Count > 1)
                    {
                        lblNumofStopReturn.Text += " (No Direct flights available yet)";
                    }
                    for (int i = 0; i < ReturnRoute.Count; i++)
                    {
                        string text = "";
                        foreach (SmallRoute route in ReturnRoute[i].RouteList)
                        {
                            text += (route.Origin.FullName + " > ");
                        }
                        text += (ReturnRoute[i].RouteList[ReturnRoute[i].RouteList.Count - 1].Destination.FullName);
                        ListItem item = new ListItem(text, i.ToString());
                        rdbRouteReturn.Items.Add(item);
                        rdbRouteReturn.SelectedIndex = 0;
                    }
                }
            }
            else
            {
                lblFlight.Text = "One Way";
                PlaceHolder1.Visible = false;
            }
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        List<BigRoute> DepartRoute = (List<BigRoute>)Session["DepartRoute"];
        Session["DepartRoute"] = DepartRoute[Convert.ToInt32(rdbRouteDepart.SelectedValue)];

        if (Session["ReturnDates"] != null)
        {
            List<BigRoute> ReturnRoute = (List<BigRoute>)Session["ReturnRoute"];
            Session["ReturnRoute"] = ReturnRoute[Convert.ToInt32(rdbRouteReturn.SelectedValue)];
        }
        else
        {
            Session["ReturnRoute"] = null;
        }

        Response.Redirect("~/SearchFlight3.aspx");
    }
}
